package demo.practice.niuke.zs.leetcode;

public class P200 {


    public int numIslands(char[][] grid) {
        int sum=0;
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[i].length;j++){
                if(grid[i][j]=='1'){
                    sum++;
                    join(grid,i,j);
                }

            }
        }
        return sum;
    }


    private void join(char[][] grid,int i,int j ){
        if(i>=grid.length|| i<0 || j>= grid[i].length || j<0 )
            return;
        if(grid[i][j]=='1'){
            grid[i][j]='2';
            join(grid,i+1,j);
            join(grid,i-1,j);
            join(grid,i,j+1);
            join(grid,i,j-1);
        }
    }
}
